package net.lab1024.sa.user.module.system.user.domain.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import net.lab1024.sa.common.common.domain.RequestUser;
import net.lab1024.sa.common.common.enumeration.UserTypeEnum;
import net.lab1024.sa.common.common.swagger.ApiModelPropertyEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Set;

/**
 * 用户表 列表VO
 *
 * @Author jabari
 * @Date 2023-06-12 14:10:13
 * @Copyright 51dev.cn
 */

@Data
public class LoginUserDetail implements UserDetails, RequestUser {

    @ApiModelProperty("token")
    private String token;

    @ApiModelProperty(value = "用户ID")
    private Long userId;

    @ApiModelPropertyEnum(UserTypeEnum.class)
    private UserTypeEnum userType;

    @ApiModelProperty(value = "用户昵称")
    private String nickname;

    @ApiModelProperty(value = "手机号")
    private String phone;

    @ApiModelProperty(value = "头像")
    private String avatarUrl;

    @JsonIgnore
    private String loginPassword;

    @ApiModelProperty("上次登录id")
    private String lastLoginIp;

    @ApiModelProperty("上次登录user-agent")
    private String lastLoginUserAgent;

    @ApiModelProperty("上次登录时间")
    private LocalDateTime lastLoginTime;

    @ApiModelProperty("请求ip")
    private String ip;

    @ApiModelProperty("请求user-agent")
    private String userAgent;

    /**
     * security 权限串
     */
    private Set<? extends GrantedAuthority> authorities;

    @Override
    public String getUserName() {
        return nickname;
    }

    @Override
    public UserTypeEnum getUserType() {
        return userType;
    }

    @Override
    public String getIp() {
        return lastLoginIp;
    }

    @Override
    public String getUserAgent() {
        return lastLoginUserAgent;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return loginPassword;
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return nickname;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}